package com.rc.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.rc.activity.R;
import com.rc.activity.dao.tbl_Category;
import com.rc.activity.dao.tbl_CategoryDao;
import com.rc.activity.dao.tbl_Item;
import com.rc.activity.dao.tbl_ItemDao;
import com.rc.activity.provider.Contract;
import com.rc.custom.MyListView;
import com.rc.util.Util;

public class CategoriesReportDetailAdapter extends BaseAdapter {

	private Context mContext;
	private String[] items;
	private long mTripID;

	private TextView txtCategories, txtTotal;
	private MyListView lvItems;

	public CategoriesReportDetailAdapter(Context c, String[] item, long mTripID) {
		mContext = c;
		this.items = item;
		this.mTripID = mTripID;
	}

	public int getCount() {
		return items.length;
	}

	public Object getItem(int position) {
		return items[position];
	}

	public long getItemId(int position) {
		return 0;
	}

	private View v;

	// create a new view for each item referenced by the Adapter
	@SuppressLint("ResourceAsColor")
	public View getView(final int position, View convertView, ViewGroup parent) {
		v = convertView;
		if (convertView == null) { // if it's not recycled, initialize some
									// attributes
			LayoutInflater vi = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.report_detail_categories_item, null);

		}
		txtCategories = (TextView) v.findViewById(R.id.txtCategories);
		txtTotal = (TextView) v.findViewById(R.id.txtTotal);
		lvItems = (MyListView) v.findViewById(R.id.lvItems);

		Cursor cursor = mContext.getContentResolver().query(
				Contract.CONTENT_URI_CATEGORY,
				null,
				tbl_CategoryDao.Properties.CategoryID.columnName + " = '"
						+ items[position] + "'", null, null);
		ArrayList<tbl_Category> arrCategory = new ArrayList<tbl_Category>();
		ArrayList<tbl_Item> arrItem = new ArrayList<tbl_Item>();
		arrCategory = tbl_Category.getObjectbyCursor(cursor);
		for (tbl_Category objCategory : arrCategory) {
			txtCategories.setText(objCategory.getName());
			cursor = mContext.getContentResolver().query(
					Contract.CONTENT_URI_ITEMS,
					null,
					tbl_ItemDao.Properties.CategoryID.columnName + " = '"
							+ objCategory.getCategoryID() + "' and "
							+ tbl_ItemDao.Properties.TripID.columnName + " = '"
							+ mTripID + "'", null, null);
			arrItem = tbl_Item.getObjectbyCursor(cursor);
			ItemsReportAdapter adpItems = new ItemsReportAdapter(mContext,
					arrItem);
			lvItems.setAdapter(adpItems);
			float total = 0;
			for (tbl_Item objItem : arrItem) {
				total = total + Util.convertFloat(objItem.getAmount());
			}
			txtTotal.setText("$" + Util.formatStringFloat(total) + "");
		}
		return v;
	}

	public void NotiDB(String[] items, long mTripID) {
		this.items = items;
		this.mTripID = mTripID;
		notifyDataSetChanged();
	}
}